
import os

from PIL import Image
import torch
isImage = lambda x: x.split('.')[-1].lower() in ['jpg', 'jepg', 'png', 'bmp']
listdir = lambda x: [os.path.join(x, f).replace('\\', '/') for f in os.listdir(x)]

IMG_EXTENSIONS = [
    'jpg', 'jpeg',
    'png', 'bmp'
]

isImage = lambda x: x.split('.')[-1].lower() in IMG_EXTENSIONS
listdir = lambda x: [os.path.join(x, f).replace('\\', '/') for f in os.listdir(x)]

def choose_device(cuda=False):
    if cuda:
        device = torch.device("cuda:0")
    else:
        device = torch.device("cpu")
    return device

def openRGBImage(file):
    if not os.path.exists(file):
        raise FileExistsError('not found!')
    if not isImage(file):
        raise RuntimeError("illegal image file!")
    image = Image.open(file).convert("RGB")
    return image

def toNormalPic(imgtensor):
    x = imgtensor * 0.5 + 0.5
    x = x.permute((1, 2, 0)).cpu().numpy()
    return x
